from OWRpy import *
import redRi18n
_ = redRi18n.get_(package = 'base')

import signals 
import redRGUI 

class coldiss(OWRpy): 
    globalSettingsList = ['commit']
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)

        # R code for generating the coldiss plot
        self.R('source(\''+unicode(os.path.join(redREnviron.directoryNames['libraryDir'], 'ecology', 'widgets', 'coldiss.R')).replace('\\', '//')+'\')')
        
        # Python variables
        self.data = {}
        self.RFunctionParam_object = ''
        
        #Inputs
        self.inputs.addInput('id0', _('R Matrix Object'), signals.base.RMatrix, self.processobject) # creer un signal pour dissimilarity object
		
        #GUI
        self.RFunctionParam_options = redRGUI.base.groupBox(self.controlArea, label='Graphic Options', orientation='horizontal')
        self.RFunctionParam_nc = redRGUI.base.spinBox(self.RFunctionParam_options, label='Number of colors: ', min = 2, max=30, value = 4 )

        self.redRByRank = redRGUI.base.radioButtons(self.RFunctionParam_options,  label = "By rank:", 
        buttons = [('Equal-sized categories'),('Equal-sized intervals')],
        setChecked='Equal-sized categories',orientation='vertical')
        
        self.redRdiag = redRGUI.base.checkBox(self.RFunctionParam_options,  label = "Options", 
        buttons = [('diag','Site labels are displayed on the diagonal.'),('win','Plot in external window.')], orientation='vertical')
        
        self.plotArea = redRGUI.plotting.redRPlot(self.controlArea, label = "Plot", displayLabel = False)
        self.plotArea.resizeCheck.uncheckAll()
        self.plotArea.topArea.hide()
        
        #Commit
        self.commit = redRGUI.base.commitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignLeft, 
        callback = self.commitFunction, processOnInput=True)
        
    def processobject(self, data):
        if not self.require_librarys(["gclus", "vegan"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''
    def commitFunction(self):
        
        if unicode(self.RFunctionParam_object) == '':
            self.status.setText('No data.')
            return

        injection = []
        if 'diag' in self.redRdiag.getCheckedIds(): 
            string='diag=TRUE'
            injection.append(string)
        else: 
            string='diag=FALSE'
            injection.append(string)
        if 'Equal-sized categories' in self.redRByRank.getChecked():
            string='byrank=TRUE'
            injection.append(string)
        else: 
            string='byrank=FALSE'
            injection.append(string)
        
        injection.append('nc='+unicode(self.RFunctionParam_nc.value()))
        inj = ','.join(injection)
        

        
        self.plotArea.plotMultiple(query = unicode(self.RFunctionParam_object)+', '+inj, function = 'coldiss')
        
        if 'win' in self.redRdiag.getCheckedIds(): 
            self.R('coldiss('+unicode(self.RFunctionParam_object)+', '+inj+')')
        
